package com.itboye.ihomebank.activity.myzhujia;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.homefragment.HouseDetailActivity;
import com.itboye.ihomebank.adapter.ShouCangAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.MyCollectionBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.WoDeFangYuanBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.SPUtils;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的收藏
 * 
 */
public class ActivityMycollection extends BaseOtherActivity implements Observer {
	View v_statusbar;
	TextView add_shap_title_tv, imgEmpty;
	ImageView close_icon;

	SmartRefreshLayout refresh;
	private ListView xlistView;

	ShouCangAdapter adapter;
	LinearLayout nodata;
	UserPresenter userPresenter;
	List<MyCollectionBean> list = new ArrayList<MyCollectionBean>();
	private String uid;
	private int pageNo = 1;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_wodefangyuan;
	}

	@Override
	protected void onInitController() {

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("我的收藏");
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		userPresenter = new UserPresenter(this);
		caozuo();
		adapter = new ShouCangAdapter(this, list, R.layout.item_shoucang);
		xlistView.setAdapter(adapter);

		xlistView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityMycollection.this,
						HouseDetailActivity.class);
//				intent.putExtra("lng",  list.get(position-1).getl);
//				intent.putExtra("lat", 0.00000);
				intent.putExtra("radius", 0);
				intent.putExtra("house_no", list.get(position).getHouseNo());
				startActivity(intent);
			}
		});

	}
	private void caozuo(){
		refresh = (SmartRefreshLayout) findViewById(R.id.refresh);
		refresh.setEnableAutoLoadmore(true);//开启自动加载功能（非必须）

		//触发自动刷新
		refresh.autoRefresh();
		refresh.setEnableAutoLoadmore(false);//禁止加载更多
		refresh.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh(RefreshLayout refreshlayout) {
				refreshlayout.getLayout().postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo = 1;
						userPresenter.myCollection(uid, "1");
					}
				}, 0);
			}
		});
		refresh.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
		refresh.finishLoadmore();
	}
	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.myCollection_success) {
				List<MyCollectionBean> bean = (List<MyCollectionBean>) entity.getData();
				if (pageNo == 1) {//下拉刷新
					list.clear();
					adapter.notifyDataSetChanged();
					//结束加载
					refresh.finishRefresh();
					//结束加载
					refresh.finishLoadmore();
				}
				if (bean.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					xlistView.setVisibility(View.GONE);
					imgEmpty.setText("暂无数据");
				} else {
					list.addAll(bean);
					nodata.setVisibility(View.GONE);
					xlistView.setVisibility(View.VISIBLE);
				}
			} else if (entity.getEventType() == UserPresenter.myCollection_fail) {
				nodata.setVisibility(View.VISIBLE);
				xlistView.setVisibility(View.GONE);
				refresh.finishLoadmore();
				refresh.finishRefresh();
			}
		}
	}
}
